package com.laf.LAF.web;

import com.laf.LAF.domain.user.User;
import com.laf.LAF.domain.user.UserRepository;
import com.laf.LAF.exception.MessageException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by lenovo on 2017/9/25.
 */
@Controller
@Slf4j
public class LoginController {

    @Autowired
    private UserRepository userRepository;

    @ApiOperation(value = "登录", notes = "")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(Model model, @RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {

        if (userRepository.findByUsername(username) == null) {
            throw new MessageException("The ID( " + username + ") existed");
        }

        // 加入一个属性，用来在模板中读取
        model.addAttribute("username", username);
        // return模板文件的名称，对应src/main/resources/templates/login.html
        log.info("{}", username + password);
        return "hello";
    }


    @GetMapping("/login")
    public String login(Model model) {

        return "login";
    }


}
